package com.yxinsur.imc.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcContractExpense;
import com.yxinsur.imc.business.domain.ImcContractExpenseRate;
import com.yxinsur.imc.business.mapper.ImcContractExpenseExpressionMapper;
import com.yxinsur.imc.business.mapper.ImcContractExpenseMapper;
import com.yxinsur.imc.business.mapper.ImcContractExpenseRateMapper;
import com.yxinsur.imc.business.service.IImcContractExpenseService;
import com.yxinsur.imc.business.vo.ImcContractExpenseVo;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 协议费用科目Service业务层处理
 *
 * @author imc
 * @date 2021-07-28
 */
@Service
public class ImcContractExpenseServiceImpl extends ServiceImpl<ImcContractExpenseMapper, ImcContractExpense> implements IImcContractExpenseService {

    @Autowired
    private ImcContractExpenseMapper imcContractExpenseMapper;
    @Autowired
    private ImcContractExpenseRateMapper imcContractExpenseRateMapper;
    @Autowired
    private ImcContractExpenseExpressionMapper imcContractExpenseExpressionMapper;

    /**
     * 根据{协议id}查询协议费用科目列表
     *
     * @param id 协议Id
     * @return List<ImcContractExpenseVo>
     */
    @Override
    public List<ImcContractExpenseVo> listImcContractExpenseByContractId(Long id) {
        return this.imcContractExpenseMapper.listImcContractExpenseByContractId(id, SecurityUtils.getTenantId());
    }

    /**
     * 删除协议费用科目
     *
     * @param contractExpenseId 协议费用科目id
     * @return 结果
     */
    @Override
    public AjaxResult deleteImcContractExpenseById(Long contractExpenseId) {
        List<ImcContractExpenseRate> imcContractExpenseRateList = imcContractExpenseRateMapper.listImcContractExpenseRateByContractExpenseId(contractExpenseId);
        if (CollUtil.isNotEmpty(imcContractExpenseRateList)) {
            return AjaxResult.error("该协议费用科目在协议产品费率中已经配置，无法删除");
        }
        // 删除协议费用科目口径
        this.imcContractExpenseExpressionMapper.deleteExpressionByContractExpenseId(contractExpenseId);
        // 删除协议费用科目
        this.imcContractExpenseMapper.deleteImcContractExpenseById(contractExpenseId);
        return AjaxResult.success();
    }
}
